﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;

using Mang24k99.Model.Dissertation;
using Mang24k99.Business.Dissertation;
using Mang24k99.Business.COMM;
using Mang24k99.Model.COMM;
using System.Collections.Generic;
using System.IO;
using Common;

namespace Mang24k99.Web.Dissertation
{
    public partial class Edit : System.Web.UI.Page
    {
        public int eheigh;
        public int ewidth;
        public int Lheigh;
        public int Lwidth;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    ViewState["editWeb"] = Request["WEBID"].ToString();
                    selectWeb.SelectedValue = ViewState["editWeb"].ToString();
                    
                }
                catch{ViewState["editWeb"] = "1"; }
                try{
                    ViewState["editID"] = Request["ID"].ToString(); //专题ID
                    ViewState["PageIndex"] = Request["PageIndex"].ToString();
                    Webtitle.Text = "修改";
                    selectWeb.Items.RemoveAt(ViewState["editWeb"].ToString() == "1" ? 1 : 0);//修改时只有一个网站
                    selectSpecialName.Enabled = false;                    
                }
                catch { ViewState["editID"] = ""; }
                if (!IsPostBack)
                {
                    
                    //selectWeb.Items.RemoveAt(ViewState["editWeb"].ToString() == "1" ? 1 : 0);//修改时只有一个网站
                    SpecialWeb.Text = ViewState["editWeb"].ToString() == "1" ? "FX168" : "24K99";
                }
                BindDissertation();
                BindSubjectPic();
            }
        }

        private void BindSubjectPic()
        {
            eheigh = int.Parse(ConfigurationManager.AppSettings["SubjectPicHeight"].ToString());
            ewidth = int.Parse(ConfigurationManager.AppSettings["SubjectPicWidth"].ToString());
            Lheigh = int.Parse(BKtCommBaseInfo.Find(eheigh).FACTOR_VALUE.ToString());
            Lwidth = int.Parse(BKtCommBaseInfo.Find(ewidth).FACTOR_VALUE.ToString());
            LlHeigh.Text = BKtCommBaseInfo.Find(eheigh).FACTOR_VALUE.ToString();
            LlWidth.Text = BKtCommBaseInfo.Find(ewidth).FACTOR_VALUE.ToString();
        }


        /// <summary>
        /// 绑定专题
        /// </summary>
        private void BindDissertation()
        {
            string webID = ViewState["editWeb"].ToString();// SpecialWeb.Text == "FX168" ? "1" : "2";            
            string hyxWebID = webID == "1" ? ConfigurationManager.AppSettings["FX168Key"].ToString() : ConfigurationManager.AppSettings["24K99Key"].ToString();
            IList<MColumnToNewsTable> mColumns = MColumnToNewsTable.Find("ColumnID", int.Parse(hyxWebID));
            string[] disID = BDissertations.GetDisID(webID,true);
            string ids = string.Empty; //已经选择的专题ID集合;
            foreach (string id in disID)
            {
                ids += id + webID + "+";
            }
            selectSpecialName.Items.Add(new ListItem("请选择专题名称", "0"));
            foreach (MColumnToNewsTable mColumn in mColumns)
            {
                if (!string.IsNullOrEmpty(ViewState["editID"].ToString()))
                {
                    if (ViewState["editID"].ToString() == mColumn.RecordID.ToString())
                    {
                        selectSpecialName.Items.Clear();//修改时只有一个专题
                        selectSpecialName.Items.Add(new ListItem((mColumn.NewsTitle.Length > 30 ? mColumn.NewsTitle.Substring(0,30) + "..." : mColumn.NewsTitle), mColumn.RecordID.ToString() + "+" + mColumn.NewsID.ToString()));
                        selectSpecialName.Items[selectSpecialName.Items.Count - 1].Selected = true;
                        MNewsContentTable aNewscontent = MNewsContentTable.Find(long.Parse(mColumn.NewsID.ToString()));
                        specialContent.Text = aNewscontent.NewsContent_Text.Length < 230 ? aNewscontent.NewsContent_Text : aNewscontent.NewsContent_Text.Substring(0, 230)+"...";
                    }
                }
                else//value值里保存RecordID+NewsID
                {
                    if (ids.IndexOf(mColumn.RecordID.ToString() + webID) < 0)//如果该专题ID不存在
                    try //无内文的标题不显示
                    {
                        MColumnToNewsTable mctt = MColumnToNewsTable.Find(mColumn.RecordID);                       
                        MNewsContentTable mnct = MNewsContentTable.Find(mctt.NewsID);
                        selectSpecialName.Items.Add(new ListItem((mColumn.NewsTitle.Length > 30 ? mColumn.NewsTitle.Substring(0, 30) + "..." : mColumn.NewsTitle), mColumn.RecordID.ToString() + "+" + mColumn.NewsID.ToString()));                        
                    }
                    catch { }
                
                }
            }
        }

        /// <summary>
        /// 更改网站
        /// </summary>
        protected void selectWeb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["editWeb"] = selectWeb.SelectedValue;
            SpecialWeb.Text = selectWeb.SelectedValue == "1" ? "FX168" : "24K99";
            selectSpecialName.Items.Clear();//更新网站后删除上个网站的选项
            BindDissertation();
        }

        /// <summary>
        /// 更改专题名称
        /// </summary>
        protected void selectSpecialName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string value = selectSpecialName.SelectedValue.Split('+')[1];
                MNewsContentTable aNewscontent = MNewsContentTable.Find(long.Parse(value));
                specialContent.Text = aNewscontent.NewsContent_Text.Length < 230 ? aNewscontent.NewsContent_Text : aNewscontent.NewsContent_Text.Substring(0, 230);
            }
            catch { specialContent.Text = "该专题无序言!"; }
        }

        /// <summary>
        /// 添加专题
        /// </summary>
        protected void OK_Click(object sender, EventArgs e)
        {
            string messageInfo = string.Empty;
            if (selectSpecialName.SelectedValue != "0")
            {
                MDissertations disInfo = new MDissertations();
                string imgPath = string.Empty;
                if (FileUploadImg.HasFile)
                {                    
                    string extension = Path.GetExtension(FileUploadImg.FileName).ToLower();
                    int size = int.Parse(FileUploadImg.PostedFile.ContentLength.ToString());
                    int Lsize = int.Parse(ConfigurationManager.AppSettings["SubjectPicSize"].ToString())*1000;
                    if (extension == ".jpg" || extension == ".gif" || extension == ".jpeg")
                    {
                        if (size > Lsize)
                        {
                            string msg = "请确定图片的大小";
                            MessageBox.Show(this, msg);
                            return;
                        }
                        else
                        {                            
                            //string strFilePath = FileUploadImg.PostedFile.FileName;
                            imgPath = ConfigurationManager.AppSettings["UploadImgPath"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + FileUploadImg.FileName.Substring(FileUploadImg.FileName.LastIndexOf('.'));
                            FileUploadImg.SaveAs(Request.PhysicalApplicationPath + imgPath);
                            System.Drawing.Image pic = System.Drawing.Image.FromFile(Request.PhysicalApplicationPath + imgPath);//strFilePath是该图片的绝对路径

                            int intWidth = pic.Width;//长度像素值
                            int intHeight = pic.Height;//高度像素值 

                            int eheigh = int.Parse(ConfigurationManager.AppSettings["SubjectPicHeight"].ToString());
                            int ewidth = int.Parse(ConfigurationManager.AppSettings["SubjectPicWidth"].ToString());
                            int Lheigh = int.Parse(BKtCommBaseInfo.Find(eheigh).FACTOR_VALUE.ToString());
                            int Lwidth = int.Parse(BKtCommBaseInfo.Find(ewidth).FACTOR_VALUE.ToString());

                            if (intWidth > Lwidth || intHeight > Lheigh)
                            {
                                string msg = "请确定图片的尺寸大小";
                                //string filename = "";
                                //filename = Request.PhysicalApplicationPath + imgPath;
                                //System.IO.File.Delete(filename);
                                MessageBox.Show(this, msg);
                                return;
                            }
                            else
                            {
                            //imgPath = ConfigurationManager.AppSettings["UploadImgPath"].ToString() + DateTime.Now.ToString("yyMMddHHmmss") + FileUploadImg.FileName.Substring(FileUploadImg.FileName.LastIndexOf('.'));
                            //FileUploadImg.SaveAs(Request.PhysicalApplicationPath + imgPath);
                            messageInfo += "文件上传成功!";

                            }

                            


                            
                        }
                        
                    }
                    else
                    {
                        Common.MessageBox.Show(this, "上传文件格式不正确!");
                        return;
                    }

                   
                }
                string[] disID = selectSpecialName.SelectedValue.Split('+');
                if (string.IsNullOrEmpty(ViewState["editID"].ToString()))  //添加专题
                {
                    disInfo.DissertationImage = imgPath;        //存放图片路径
                    disInfo.DissertationWeb = SpecialWeb.Text == "FX168" ? 1 : 2;
                    disInfo.DissertationID = int.Parse(disID[0]);
                    disInfo.DissertationNewsID = long.Parse(disID[1]);
                    disInfo.BDeleted = 1;
                    disInfo.DissertationNumber = 26;//没上线;
                    BDissertations.Add(disInfo);
                    selectSpecialName.SelectedValue = "0";
                    specialContent.Text = "";
                    messageInfo += "专题添加成功!";
                    Common.MessageBox.ShowAndRedirect(this, messageInfo, "ChildNews/List.aspx?ID=26&AddNewsDisID=" + disID[0] + "&webID=" + (SpecialWeb.Text == "FX168" ? "1" : "2"));
                }
                else//修改专题
                {
                    if (!string.IsNullOrEmpty(imgPath))//如果更新了上传图片
                    {
                        disInfo = MDissertations.Find(int.Parse(disID[0]));
                        disInfo.DissertationImage = imgPath;
                        disInfo.Update();
                        messageInfo += "图片成功更新!";
                    }
                    Common.MessageBox.ShowAndRedirect(this, string.IsNullOrEmpty(messageInfo)?"专题没有修改!":messageInfo, "List.aspx?WEBID=" + ViewState["editWeb"].ToString() + "&PageIndex=" + ViewState["PageIndex"].ToString());
                }
            }
            else
                Common.MessageBox.Show(this, "请选择正确的专题!");
        }

       

        protected void Cancel_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ViewState["editID"].ToString()))
                Response.Redirect("List.aspx");
            else
                Response.Redirect("List.aspx?WEBID=" + ViewState["editWeb"].ToString() + "&PageIndex=" + ViewState["PageIndex"].ToString());
        }
       
    }
}